package cn.java79.springboot.zsb.ctrl;

import cn.java79.springboot.zsb.config.Email;
import cn.java79.springboot.zsb.config.OssConfig;
import cn.java79.springboot.zsb.entity.Advtise;
import cn.java79.springboot.zsb.pojo.AjaxPage;
import cn.java79.springboot.zsb.pojo.AjaxResult;
import cn.java79.springboot.zsb.service.AdvitseService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author oldliu
 * @since 1.0
 */
@RestController
@RequestMapping("advtise")
@Slf4j
@Api("广告管理")
public class AdvtiseController {
    @Autowired
    private AdvitseService advtiseService;
    @PostMapping("add")
    //RequestBody请求的数据是json格式
    public AjaxResult add(@RequestBody Advtise a){
        boolean b = this.advtiseService.save(a);
        return AjaxResult.judge(b);
    }
    @GetMapping("search")
    public AjaxPage<Advtise> search(String title,String url, Date starttime,Date end, @RequestParam(name = "page", defaultValue = "1") int page, @RequestParam(name = "limit", defaultValue = "10") int limit) {
        log.info("查询的条件有：{},{},{},{},{}",title,url,starttime,end,page);
        return AjaxPage.convert(this.advtiseService.search(title,url, starttime,end, page, limit));
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
